package com.imay.live.activity.discovery;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.imay.live.adapter.AllHotLabelLinearAdapter;
import com.imay.live.fragment.discovery.DisCoveryAssortFragment;
import com.imay.live.helper.HotLabelCallBack;
import com.imay.live.model.HotLabelModel;
import com.imay.live.proto.Result;
import com.imay.live.proto.ResultCode;
import com.imay.live.proto.helper.ProtoCallBack;
import com.imay.live.proto.helper.ProtoHttpFactory;
import com.imay.live.proto.helper.ProtoRequestHelper;
import com.imay.live.R;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;

/**
 * Created by liangzhenxiong on 2017/04/10 0010.
 * 发现-分类-标签详情列表
 */

public class HotLabelActivity extends DicLoadActivity implements HotLabelCallBack {
    final static String LABELS = "labels";

    public static void start(Activity context, ArrayList<String> labels) {
        Intent starter = new Intent(context, HotLabelActivity.class);
        starter.putStringArrayListExtra(LABELS, labels);
        context.startActivityForResult(starter, DisCoveryAssortFragment.REQUE_REFRSH_TITLE);
    }

    @Override
    public void onRefreshLoad() {
        ProtoRequestHelper.task(ProtoHttpFactory.getHotLabel()).setTag(this.hashCode()).request(new ProtoCallBack() {
            @Override
            public void onRequestSuccess(String command, String data) {
                try {
                    Result<ArrayList<String>> result = JSON.parseObject(data, new TypeReference<Result<ArrayList<String>>>() {
                    }.getType());
                    if (result.result == ResultCode.CodeSuccess && result.data != null && result.data.size() > 0) {
                        ArrayList<HotLabelModel> models = new ArrayList<HotLabelModel>();
                        for (String name : result.data) {
                            HotLabelModel hotLabelModel = new HotLabelModel();
                            hotLabelModel.setLabel(name);
                            models.add(hotLabelModel);
                        }
                        loadViewControl.refreshLoadView(models, 0, false);

                    } else {
                        loadViewControl.loadError(getResources().getString(R.string.str_loaderror));
                    }

                } catch (Exception e) {
                    loadViewControl.loadError(getResources().getString(R.string.str_loaderror));
                }


            }

            @Override
            public void onRequestError(String command, int code, String message) {
                loadViewControl.loadError(getResources().getString(R.string.str_loaderror));
            }
        });

    }

    @Override
    protected void initAdapte() {
        super.initAdapte();
        mAdapter = new AllHotLabelLinearAdapter(mdatas, this);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        com.imay.live.utils.ViewUtil.setForgroundRadius($(R.id.container));
        mrecyclerView.setBackgroundColor(getResources().getColor(R.color.bg_window));
        setTitle(getResources().getString(R.string.str_assortmenthot_text));
        ArrayList<String> labels = new ArrayList<>();
        if (labels != null) {
            ArrayList<HotLabelModel> moreModels = new ArrayList<>(labels.size());
            for (String label : labels) {
                HotLabelModel hotLabelModel = new HotLabelModel();
                hotLabelModel.setLabel(label);
                moreModels.add(hotLabelModel);
            }
            loadViewControl.refreshLoadView(moreModels, 0);
            loadViewControl.setEnableLoadmore(false);
            mrecyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this)
                    .size(0)
                    .build());
        }
        onRefreshLoad();
        //loadViewControl.setEnableLoadmore(false);
    }

    @Override
    public void onClickTitle(String title) {
        HotLabelDetailActivity.start(this, title);
    }

    @Override
    public void onClickSuggest(ArrayList<String> labels) {

    }
}
